package p3.cse;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * This is the first activity executed and will determine if this is the first
 * run, and if it is will either allow user to download a backup or initialize a
 * blank database.
 */
public final class Main extends Activity {
	/**
	 * The click listener for attempting to download a backup of the password
	 * database.
	 */
	private OnClickListener downloadfile = new OnClickListener() {
		public void onClick(final View v) {
			Backup.downloading = true;
			startActivity(new Intent(Main.this, Backup.class));
			finish();
		}
	};

	/**
	 * Click Listener setting up the password for the first time. If it is
	 * valid, unlock the screen and start the main activity. Otherwise, display
	 * an error message in red.
	 */
	private OnClickListener acceptMasterPasswordListener = new OnClickListener() {
		public void onClick(final View v) {
			TextView input = (TextView) findViewById(R.id.masterPasswordin);
			String inputpwd = input.getText().toString();

			String securityCheck = Filer.checkStrength(inputpwd);

			if (securityCheck != null) {
				Log.d("password", inputpwd);
				TextView error = (TextView) findViewById(R.id.pwderror);
				error.setText(securityCheck);
				error.setTextColor(Color.RED);
				error.setVisibility(1);
			} else {
				EditText masterPassword = (EditText) findViewById(R.id.masterPasswordin);
				try {
					Filer filer = Filer.getInstance();
					filer.init(masterPassword.getText().toString());
					filer.writeFile();
				} catch (IOException e) {
					Log.e("Main", "Could not write password database", e);
				}

				startActivity(new Intent(Main.this, Unlocked.class));
				finish();
			}
		}
	};

	/**
	 * Primary entry point for the application.
	 * 
	 * {@inheritDoc}
	 */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/**
		 * This is a returning user
		 */
		if (Filer.DB_FILE.exists()) {
			Log.i("Main", "Found a password database");
			// if the file exists, go to the unlock screen.
			startActivity(new Intent(Main.this, Unlock.class));
			finish();
			/**
			 * First time user, enter account setup
			 */
		} else {
			setContentView(R.layout.setup);

			Button ok = (Button) findViewById(R.id.okButton);
			ok.setOnClickListener(acceptMasterPasswordListener);

			Button restore = (Button) findViewById(R.id.downloadButton);
			restore.setOnClickListener(downloadfile);
		}
	}

	/**
	 * If the application is pause, we want to close it. TODO This does seem to
	 * actually do anything.
	 */
	@Override
	public void onPause() {
		super.onPause();
		finish();
	}

	/**
	 * If the application is stopped, we want to close it.
	 */
	@Override
	public void onStop() {
		super.onStop();
		finish();
	}
}
